package com.alibaba.algo.components.yapi.handler;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.algo.components.yapi.annotations.ApiModelProperty;
import com.alibaba.algo.components.yapi.entity.Node;
import com.alibaba.algo.components.yapi.util.ClazzUtils;
import com.alibaba.algo.components.yapi.vo.ReqBodyForm;
import com.alibaba.algo.components.yapi.vo.ReqHeaders;
import com.alibaba.algo.components.yapi.vo.RequestHandlerResponseVO;
import com.alibaba.fastjson.JSON;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class FormRequestHandler implements RequestHandler {

	@Override
	public RequestHandlerResponseVO handler(Method method) {
		RequestHandlerResponseVO responseVO = new RequestHandlerResponseVO();
		List<ReqBodyForm> reqBodyFormList = new ArrayList<ReqBodyForm>();
		Parameter[] params = method.getParameters();
		String contentType = MediaType.APPLICATION_FORM_URLENCODED_VALUE;
		if (params != null && params.length > 0) {
			for (Parameter parameter : params) {
				Class<?> modelClass = parameter.getType();
				if (modelClass == MultipartFile.class || ClazzUtils.simpleClass(modelClass)) {
					ReqBodyForm reqBodyForm = new ReqBodyForm();
					reqBodyForm.setName(modelClass.getSimpleName());
					reqBodyForm.setRequired(0);
					// 获取参数注解
					ApiModelProperty apiModelProperty = parameter.getAnnotation(ApiModelProperty.class);
					if (apiModelProperty != null) {
						reqBodyForm.setDesc(apiModelProperty.value());
						reqBodyForm.setRequired(apiModelProperty.required() == true ? 1 : 0);
						reqBodyForm.setExample(apiModelProperty.example());
					}
					if (modelClass == MultipartFile.class) {
						contentType = MediaType.MULTIPART_FORM_DATA_VALUE;
						reqBodyForm.setType("file");
					} else if (ClazzUtils.simpleClass(modelClass)) {
						reqBodyForm.setType("text");
					}
					reqBodyFormList.add(reqBodyForm);
				} else {
					List<Node> nodeList = ClazzUtils.getChildrenNodes(modelClass, new HashMap<String, String>());
					log.info("nodeList:{}", JSON.toJSONString(nodeList));
					for (Node node : nodeList) {
						ReqBodyForm reqBodyForm = new ReqBodyForm();
						reqBodyForm.setName(node.getFieldName());
						reqBodyForm.setRequired(node.isRequired() == true ? 1 : 0);
						reqBodyForm.setDesc(node.getDesc());
						reqBodyForm.setType("text");
						reqBodyFormList.add(reqBodyForm);
					}
				}
			}
		}
		List<ReqHeaders> reqHeaders = new ArrayList<ReqHeaders>();
		ReqHeaders reqHeader = new ReqHeaders();
		reqHeader.setName("Content-Type");
		reqHeader.setValue(contentType);
		reqHeaders.add(reqHeader);
		responseVO.setReqHeaders(reqHeaders);
		responseVO.setReqBodyForm(reqBodyFormList);
		responseVO.setReqBodyType("form");
		responseVO.setContentType(contentType);
		responseVO.setMethodUri(ClazzUtils.getMethodUri(method));
		responseVO.setMethodType("POST");
		return responseVO;
	}

}
